/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UCAdmin_View.java
 *
 * Created on 2009-11-17, 2:17:32
 */

package ucm.View;
import javax.swing.JOptionPane;
import java.sql.*;
import cp3120_p.DAO;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Administrator
 */
public class UCManager_View extends javax.swing.JFrame {
    private DAO DbConn;
    Vector headair = new Vector();
    Vector dataair = new Vector();
    Vector headfight = new Vector();
    Vector datafight = new Vector();
    /** Creates new form UCAdmin_View */
    public UCManager_View() {
        initComponents();
        DbConn = new DAO();
        headair.add("AirPoart ID");
        headair.add("AirPoart Name");
        headair.add("AirPoart TAX");
        headair.add("GST");
        GetAirPoartInfo(false);
        headfight.add("Fight ID");
        headfight.add("Fight Destination");
        headfight.add("Fight From");
        headfight.add("Fight Discount");
        headfight.add("Fight Price");
        GetFightInfo(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Add");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Delete");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(jTable2);

        jLabel1.setText("AirPoart Infomation");

        jLabel2.setText("Fight Information");

        jButton3.setText("UpDateToDB");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Add");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("UpDateToDB");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Delete");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(144, 144, 144)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 315, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(230, 230, 230))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton6))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(160, 160, 160)
                        .addComponent(jButton4)
                        .addGap(18, 18, 18)
                        .addComponent(jButton5)
                        .addGap(37, 37, 37)
                        .addComponent(jButton2)
                        .addGap(117, 117, 117))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton4)
                    .addComponent(jButton5)
                    .addComponent(jButton1)
                    .addComponent(jButton3)
                    .addComponent(jButton6))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        SaveAirPoartInfo();
        GetAirPoartInfo(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        SaveAirPoartInfo();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        SaveFightInfo();
        GetFightInfo(true);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        SaveFightInfo();
        GetFightInfo(false);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        Connection con = DbConn.getConnection();
        if (con == null) {
            return;
        }
        try {
            String name = jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            stmt.executeUpdate("DELETE FROM destinations WHERE ID = '"
                        + name + "'");
                JOptionPane.showMessageDialog(null, "del Successful!");

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Selet a AirPoart!");
            return;
        }
        dataair.clear();
        GetAirPoartInfo(false);
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Connection con = DbConn.getConnection();
        if (con == null) {
            return;
        }
        try {
            String name = jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            stmt.executeUpdate("DELETE FROM flight WHERE FLIGHTID = '"
                        + name + "'");
                JOptionPane.showMessageDialog(null, "del Successful!");

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Selet a Flight!");
            return;
        }
        dataair.clear();
        GetAirPoartInfo(false);
    }//GEN-LAST:event_jButton2ActionPerformed
public void GetAirPoartInfo(boolean addnew)
{
           Connection con = DbConn.getConnection();
        DefaultTableModel model = new DefaultTableModel();
        if (con == null) {
            return;
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            ResultSet res;
            res = stmt.executeQuery("select * from destinations");
            if (res.first()) {
                Vector v1 = new Vector();
                v1.add(res.getString("ID"));
                v1.add(res.getString("Destination"));
                v1.add(res.getString("AIRPOART_TAX"));
                v1.add(res.getString("GOOD_TAX"));
                dataair.add(v1);
            }
            while (res.next()) {
                Vector v = new Vector();
                 v.add(res.getString("ID"));
                v.add(res.getString("Destination"));
                v.add(res.getString("AIRPOART_TAX"));
                v.add(res.getString("GOOD_TAX"));
                dataair.add(v);
            }
            if(addnew)
            {
                Vector v2 = new Vector();
                 v2.add("Input");
                v2.add("New");
                v2.add("Air Poart");
                v2.add("InforMaiton");
                dataair.add(v2);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Exception");
            return;
        }
        model.setDataVector(dataair, headair);
        jTable1.setModel(model);
}
public void SaveAirPoartInfo()
{
        Connection con = DbConn.getConnection();
        if (con == null) {
            return;
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            stmt.executeUpdate("DELETE FROM destinations");
            int totlelan = jTable1.getRowCount();
            for(int i = 0; i < totlelan; i++)
            {
                String ID = jTable1.getValueAt(i, 0).toString();
                String Destination = jTable1.getValueAt(i, 1).toString();
                String tax1 = jTable1.getValueAt(i, 2).toString();
                String tax2 = jTable1.getValueAt(i, 3).toString();
                stmt.executeUpdate("INSERT INTO destinations (ID,Destination,AIRPOART_TAX,GOOD_TAX)" +
                        " values('" + ID + "', '" + Destination + "','" + tax1 + "','" + tax2 +"')");
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
            return;
        }
        dataair.clear();
}
public void GetFightInfo(boolean addnew)
{
           Connection con = DbConn.getConnection();
        DefaultTableModel model = new DefaultTableModel();
        if (con == null) {
            return;
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            ResultSet res;
            res = stmt.executeQuery("select * from flight");
            if (res.first()) {
                Vector v1 = new Vector();
                v1.add(res.getString("FLIGHTID"));
                v1.add(res.getString("FLIGHTDESTINATION"));
                v1.add(res.getString("FLIGHTFROM"));
                v1.add(res.getString("FLIGHTSTATE"));
                v1.add(res.getString("PRICE"));
                datafight.add(v1);
            }
            while (res.next()) {
                Vector v = new Vector();
                 v.add(res.getString("FLIGHTID"));
                v.add(res.getString("FLIGHTDESTINATION"));
                v.add(res.getString("FLIGHTFROM"));
                v.add(res.getString("FLIGHTSTATE"));
                v.add(res.getString("PRICE"));
                datafight.add(v);
            }
            if(addnew)
            {
                Vector v2 = new Vector();
                 v2.add(" ");
                v2.add(" ");
                v2.add(" ");
                v2.add(" ");
                v2.add(" ");
                datafight.add(v2);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Exception");
            return;
        }
        model.setDataVector(datafight, headfight);
        jTable2.setModel(model);
}

public void SaveFightInfo()
{
        Connection con = DbConn.getConnection();
        if (con == null) {
            return;
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            stmt.executeUpdate("DELETE FROM flight");
            int totlelan = jTable1.getRowCount();
            for(int i = 0; i < totlelan; i++)
            {
                String ID = jTable1.getValueAt(i, 0).toString();
                String to = jTable1.getValueAt(i, 1).toString();
                String from = jTable1.getValueAt(i, 2).toString();
                String Discount = jTable1.getValueAt(i, 3).toString();
                String Price = jTable1.getValueAt(i, 3).toString();
                stmt.executeUpdate("INSERT INTO flight (FLIGHTID,FLIGHTDESTINATION,FLIGHTFROM,FLIGHTSTATE,PRICE)" +
                        " values('" + ID + "', '" + to + "','" + from + "','" + Discount+ "','" + Price +"')");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
            return;
        }
        datafight.clear();
}

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UCManager_View().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    // End of variables declaration//GEN-END:variables

}
